/*
 * @Author: jiu yin
 * @Date: 2024-04-09 11:33:09
 * @LastEditTime: 2024-04-24 14:14:44
 * @LastEditors: jiu yin zhen jing
 * @FilePath: \web-smart-tour\src\utils\request.js
 * jiu
 */

import { getToken,setToken } from './index'
export let baseURL = process.env.VUE_APP_BASE_API;//dev
export let baseURLAi = process.env.VUE_APP_BASE_API_AI;//dev
export let NODE_ENV = process.env.NODE_ENV;//dev
export const MOCK_OPEN = process.env.MOCK_OPEN;//dev


import store from "@/store"
const request = (options) => {
    let token = getToken();
    // let token = "84U3&D{WC0c305566d-0a75-4c8c-c487-182cb7049b92%WGH]ZSJ3Ux*tut";
    function create(resolve, reject) {
        uni.request({
            url: (options.baseURL || baseURL) + options.url, //接口地址：前缀+方法中传入的地址
            method: options.method || 'GET', //请求方法：传入的方法或者默认是“GET”
            data: options.data || {}, //传递参数：传入的参数或者默认传递空集合
            dataType: options.dataType || "json",
            header: {
                // 'Authorization': `Bearer ${token}`, //自定义请求头信息
                'token': `${token}`, //自定义请求头信息
                ...options.header
            },
            success: (res) => {
				console.log("api_suc===", options.url, res);
                if (res.data.code == 920) {//用户另一端登录
                    store.dispatch("clearLoginInfo");
					store.commit("userInfo/set_is_login_popup", true);
                    Promise.reject("登录过期")
                } else if (res.data.code == 0 || res.data.code == 500) {
                    uni.$u.toast(res.data.msg);
                } else if (res.data.code == 400) {
                    uni.$u.toast(res.data.msg);

                } else {
                    resolve(res.data);
                }

            },
            // 这里的接口请求，如果出现问题就输出接口请求失败
            fail: (err) => {
                console.error(err);
                uni.$u.toast(JSON.stringify(err));
				console.log("api_err===", err);
                Promise.reject(JSON.stringify(err))
            }
        });
    }
    return new Promise(create);
};


export default request;
